package hellicopter_tester;


import hellicopter_tester.Hellicopter_tester;
import hellicopter_tester.UDP;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Törcsi
 */
public class Controller extends javax.swing.JPanel {

    /**
     * Creates new form Controller
     */
    public Controller() {
        initComponents();
    }
    byte gaz=0;
    byte fordulas=8;
    byte dontes=8;
    public byte[] concat()
    {
        byte[] ret= new byte[4];
        ret[0]=gaz;
        ret[1]=(byte) (((int)fordulas<<4)+(int)dontes);
        ret[2]=13;
        ret[3]=10;
        jTextField1.setText(toHex(ret));
        return ret;
    }
    public static String toHex(byte[] ba)
    {
        String ret="";
        for(int i = 0; i < ba.length; i++)
        {
            String var=String.format("%x", ba[i]);
            if(var.length()==1)
               ret=ret+'0';
            ret=ret + var;
        }
        return ret;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jCheckBox1 = new javax.swing.JCheckBox();

        jButton1.setText("Előre");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                elorePress(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                eloreRelease(evt);
            }
        });

        jButton2.setText("Hátra");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                hatraPress(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                hatraRelease(evt);
            }
        });

        jButton3.setText("Balra");
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                balraPress(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                balraRelease(evt);
            }
        });

        jButton4.setText("Jobbra");
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jobbraPress(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jobbraRelease(evt);
            }
        });

        jButton5.setText("Feljebb");
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                felPress(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                felRelease(evt);
            }
        });

        jButton6.setText("Lejjebb");
        jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                lePress(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                leRelease(evt);
            }
        });

        jTextField1.setEditable(false);

        jLabel1.setText("Aktuális kimenet:");

        jButton7.setText("Indít!");
        jButton7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                inditClk(evt);
            }
        });

        jButton8.setText("Leállít!");
        jButton8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                allClk(evt);
            }
        });

        jTextField2.setText("192.168.2.254");

        jCheckBox1.setText("getRecive");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton8)
                            .addComponent(jButton7))
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton3)
                                .addGap(58, 58, 58)
                                .addComponent(jButton4))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(56, 56, 56)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton5)
                            .addComponent(jButton6)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCheckBox1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton7)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton5)
                            .addGap(37, 37, 37)
                            .addComponent(jButton6))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addGap(8, 8, 8)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton3)
                                .addComponent(jButton4))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton2)
                                .addComponent(jButton8)))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jCheckBox1)
                        .addContainerGap(22, Short.MAX_VALUE))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void elorePress(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_elorePress
            dontes+=4;
            try {
            UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_elorePress

    private void eloreRelease(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_eloreRelease
        dontes=8;
       try {
               UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_eloreRelease

    private void hatraPress(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_hatraPress
        dontes-=4;
        try {
               UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_hatraPress

    private void hatraRelease(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_hatraRelease
       dontes=8;
       try {
               UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_hatraRelease

    private void balraPress(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_balraPress
        fordulas-=4;
        try {
              UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_balraPress

    private void balraRelease(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_balraRelease
        fordulas=8;
        try {
              UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_balraRelease

    private void jobbraPress(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jobbraPress
       fordulas+=4;
       try {
             UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jobbraPress

    private void jobbraRelease(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jobbraRelease
        fordulas=8;
        try {
            UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jobbraRelease

    private void felPress(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_felPress
        if(gaz<=123)
            gaz+=4;
        try {
              UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_felPress

    private void felRelease(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_felRelease
        // TODO add your handling code here:
    }//GEN-LAST:event_felRelease

    private void lePress(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lePress
       if(gaz>=4)
           gaz-=4;
       try {
              UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_lePress

    private void leRelease(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_leRelease
        // TODO add your handling code here:
    }//GEN-LAST:event_leRelease

    private void inditClk(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_inditClk
        // TODO add your handling code here:
        try {
            // TODO code application logic here
            gaz=(byte) 129;
              UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
            gaz=2;
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_inditClk

    private void allClk(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_allClk
        try {
            // TODO code application logic here
            gaz=(byte) 128;
               UDP.start(10001,jTextField2.getText(),jCheckBox1.isEnabled(),concat());
            gaz=2;
        } catch (Exception ex) {
            Logger.getLogger(Hellicopter_tester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_allClk

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
